package com.example.common.room

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * 评论草稿数据仓库
 */
class CommentDraftRepository(private val commentDraftDao: CommentDraftDao) {
    
    /**
     * 保存评论草稿
     */
    suspend fun saveDraft(videoId: Int, content: String) {
        val draft = CommentDraft(
            videoId = videoId,
            content = content,
            saveTime = System.currentTimeMillis()
        )
        withContext(Dispatchers.IO) {
            commentDraftDao.insertOrUpdate(draft)
        }
    }
    
    /**
     * 获取指定视频的评论草稿
     */
    suspend fun getDraftByVideoId(videoId: Int): CommentDraft? {
        return withContext(Dispatchers.IO) {
            commentDraftDao.getDraftByVideoId(videoId)
        }
    }
    
    /**
     * 删除指定视频的评论草稿
     */
    suspend fun deleteDraftByVideoId(videoId: Int) {
        withContext(Dispatchers.IO) {
            commentDraftDao.deleteDraftByVideoId(videoId)
        }
    }
    
    /**
     * 删除所有评论草稿
     */
    suspend fun deleteAllDrafts() {
        withContext(Dispatchers.IO) {
            commentDraftDao.deleteAllDrafts()
        }
    }
}